import Vue from 'vue'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import MainPage from './components/app.vue'


import './assets/css/style.css'
import './assets/css/element-ui.css'
import './assets/css/layer.css'
import './plugins/axios.js'
import commit from './commit/commit'
import global_info from './assets/js/url.js' //1
import {isAuth} from './assets/js/isAlon'

import moment from 'moment'
import axios from 'axios'
import qs from "qs"
import store from './store/store'

import Vuex from 'vuex'
import router from './router/index'


Vue.use(Vuex)
Vue.prototype.$commit = commit;
Vue.prototype.qs = qs;
Vue.prototype.GLOBAL = global_info;

// Vue.component('v-distpicker', VDistpicker)
Vue.use(moment);
moment.locale('zh-cn'); //设置语言 或 moment.lang('zh-cn');
Vue.prototype.$moment = moment; //赋值使用

Vue.prototype.$axios = axios
Vue.prototype.isAuth = isAuth // 权限方法
Vue.use(ElementUI);

Vue.config.silent = true
// import layer from "layui-layer";

window.routeBool = false
console.log("下面是window对象");
console.log(window);


Vue.prototype.validForbid = function (value, number = 255) {
    value = value.replace(/[`~!@#$%^&*()_\-+=<>?:"{}|,./;'\\[\]·~！@#￥%……&*（）——\-+={}|《》？：“”【】、；‘’，。、]/g, '').replace(/\s/g, "");
    if (value.length >= number) {
        this.$message({
            type: "warning",
            message: `输入内容不能超过${number}个字符`
        });
    }
    return value;
}
const on = Vue.prototype.$on

Vue.prototype.$on = function (event, func) {
    let previous = 0
    let newFunc = func
    if (event === 'click') {
        newFunc = function () {
            const now = new Date().getTime()
            if (previous + 1000 <= now) {
                func.apply(this, arguments)
                previous = now
            }
        }
    }
    on.call(this, event, newFunc)
}

new Vue({
    el: '#app',
    router,
    store,
    render: h => h(MainPage),
});


//全局路由守卫
router.beforeEach((to, from, next) => {
    /*路由发生变化修改页面title*/
    if (to.meta.title) {
        document.title = to.meta.title;
    }
    debugger;
    if (to.path === "/login") {
        next();
    }
    let token = localStorage.getItem('token');
    console.log("token:" + token);
    if (token == null || token === '') {
        next({
            path: '/',
            query: {redirect: to.fullPath}
        })
    } else {
        next();
    }
});
